import { login, getProfile, getUserInfo } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
export default {
  // 开启命名空间
  namespaced: true,
  state: {
    Token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, newValue) {
      state.Token = newValue
      setToken(newValue)
    },
    setUserInfo(state, newValue) {
      state.userInfo = newValue
    }
  },
  actions: {
    async userLogin(context, payload) {
      const res = await login(payload)
      // console.log('payload', payload)
      // console.log('res', res.data)
      if (res.code === 10000) {
        context.commit('setToken', res.data)
      }
    },
    async userInfo(context, payload) {
      const res = await getProfile()
      const userId = res.data.userId
      const uInfo = await getUserInfo(userId)
      context.commit('setUserInfo', uInfo.data)
    },
    // 退出登录
    logout(context) {
      // 清除token
      context.commit('setToken', '')
      // 清除用户信息
      context.commit('setUserInfo', {})
    }
  }
}
